package com.vpen.controller;

import cn.hutool.core.date.DateUtil;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.Deflater;
import java.util.zip.Inflater;

/**
 * @version: 1.0
 * @author: Vpen
 * @date: 2021/11/1 22:02
 * @content:
 */
public class a {
    public static void main(String[] args) throws IOException {
//        String json = "我是字符abc123";
//
//
//        // 获取json的byte[]
//        byte[] jsonBytes = json.getBytes();
//
//        // 压缩
//        byte[] enJson = encode(jsonBytes);
//        // 压缩返回base64
//        byte[] encode = Base64.getEncoder().encode(enJson);
//        // 压缩要传输的json
//        String s = new String(encode);
//
//
//
//        // 解压获取到的Base64
//        byte[] dejson = Base64.getDecoder().decode(s.getBytes());
//        // 解压缩
//        String decode = decode(dejson);
//        System.out.println(decode);

//        List<String> list = new ArrayList<>();
//        list.add("1.1.1");
//        list.add("1.2");
//        list.add("2.1.2");
//        List<String> strings = list.stream().sorted().collect(Collectors.toList());
//        System.out.println(strings);

        LocalDateTime day = LocalDate.now().atStartOfDay();
        LocalDateTime end = day.plusHours(24);
        while (day.isBefore(end)){
            System.out.println(day);
            day=day.plusHours(3);
        }



    }



    /**
     * @author:韦鹏
     * @desc: 解压
     * */
    public static String decode(byte[] data) {
        byte[] output = new byte[0];
        try {
            Inflater inflater = new Inflater();
            inflater.reset();
            inflater.setInput(data);

            ByteArrayOutputStream o = new ByteArrayOutputStream(data.length);
            try {
                byte[] buf = new byte[1024];
                while (!inflater.finished()) {
                    int i = inflater.inflate(buf);
                    o.write(buf, 0, i);
                }
                output = o.toByteArray();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    o.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            inflater.end();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new String(output);
    }


    /**
     * @author:韦鹏
     * @desc: 解压
     * */
    public static String uncompress(byte[] input) throws IOException {
        Inflater inflater = new Inflater();
        inflater.setInput(input);
        ByteArrayOutputStream baos = new ByteArrayOutputStream(input.length);
        try {
            byte[] buff = new byte[1024];
            while (!inflater.finished()) {
                int count = inflater.inflate(buff);
                baos.write(buff, 0, count);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            baos.close();
        }
        inflater.end();
        byte[] output = baos.toByteArray();
        return new String(output, "UTF-8");
    }

    /**
     * @author:韦鹏
     * @desc: 压缩
     * */
    public static byte[] encode(byte[] data) throws IOException {
        byte[] output;
        Deflater compress = new Deflater(4,false);

        compress.reset();
        compress.setInput(data);
        compress.finish();
        try (ByteArrayOutputStream bos = new ByteArrayOutputStream(data.length)) {
            byte[] buf = new byte[1024];
            while (!compress.finished()) {
                int i = compress.deflate(buf);
                bos.write(buf, 0, i);
            }
            output = bos.toByteArray();
        } catch (Exception e) {
            output = data;
            e.printStackTrace();
        }
        compress.end();
        return output;
    }


    /**
     * 对象转数组
     * @param obj
     * @return
     */
    public static byte[] toByteArray (Object obj) {
        byte[] bytes = null;
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            ObjectOutputStream oos = new ObjectOutputStream(bos);
            oos.writeObject(obj);
            oos.flush();
            bytes = bos.toByteArray ();
            oos.close();
            bos.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return bytes;
    }

    /**
     * 数组转对象
     * @param bytes
     * @return
     */
    public static String toObject (byte[] bytes) {
        Object obj = null;
        try {
            ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
            ObjectInputStream ois = new ObjectInputStream(bis);
            obj = ois.readObject();
            ois.close();
            bis.close();
        } catch (IOException | ClassNotFoundException ex) {
            ex.printStackTrace();
        }
        return obj.toString();
    }





}
